let handleTouchStart, handleTouchMove

export default {
  mounted (el, binding) {
    let startX, startY

    handleTouchStart = (e) => {
      startX = e.touches[0].clientX
      startY = e.touches[0].clientY
    }

    handleTouchMove = (e) => {
      const deltaX = e.touches[0].clientX - startX
      const deltaY = e.touches[0].clientY - startY

      if ((Math.abs(deltaX) > Math.abs(deltaY)) && Math.abs(deltaX) > 30) {
        if (deltaX < 0) {
          // 向左滑动
          binding.value && typeof binding.value === 'function' && binding.value(binding.arg)
        }
      }
    }

    el.addEventListener('touchstart', handleTouchStart, {
      passive: false
    })
    el.addEventListener('touchmove', handleTouchMove, {
      passive: false
    })
  },
  unmounted (el) {
    el.removeEventListener('touchstart', handleTouchStart)
    el.removeEventListener('touchmove', handleTouchMove)
  }
}
